let router = require('../router/Router');
let exchangeService = require('../service/exchangeService');

const { BusiError, SysError } = require('../exception/exception');


router.post("/auth/ex/record", function (req, resp) {
    let page = req.body.page;
    if (!page) {
        resp.send({code: 500, msg: "page invalid"});
        return;
    }

    page = parseInt(page);

    exchangeService.records(req.uid, page).then(res => {
        resp.send({code: 200, msg: "success", data: res});
    }).catch(e => {
        console.logw(e);
        resp.send({code: 500, msg: e});
    });
});

router.post("/auth/ex/alipay", function (req, resp) {
    let amt = req.body.amt;
    amt = parseFloat(amt).toFixed(2);
    if (amt <= 0) {
        resp.send({code: 500, msg: "amt invalid"});
        return;
    }

    let dto = {
        uid: req.uid,
        exAmt: amt,
        type: "A0000",
    };

    exchangeService.exchange(dto).then(res => {
        if (res != null) {
            resp.send({code: 200, msg: "success", data: res});
        } else {
            resp.send({code: 500, msg: ""});
        }
    }).catch(e => {
        console.logw("err:", e);
        resp.send({code: 500, msg: e});
    });
});


router.post("/auth/ex/bankCard", function (req, resp) {
    let amt = req.body.amt;
    amt = parseFloat(amt).toFixed(2);
    if (amt <= 0) {
        resp.send({code: 500, msg: "amt invalid"});
        return;
    }

    let dto = {
        uid: req.uid,
        exAmt: amt,
        type: "B0000",
    };

    exchangeService.exchange(dto).then(res => {
        if (res > 0) {
            resp.send({code: 200, msg: "success", data: res});
        } else {
            resp.send({code: 500, msg: ""});
        }
    }).catch(e => {
        console.logw("err:", e);
        resp.send({code: 500, msg: ""});
    });
});